﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace xCarLinkMP3Organizer
{
    public class UsbDrive : BaseViewModel
    {
        private const int Kb = 1024;
        private const int Mb = Kb * 1000;
        private const int Gb = Mb * 1000;
        
        public UsbDrive(string name)
        {
            Name = name;
            Model = String.Empty;
            Volume = String.Empty;
            FreeSpace = 0;
            Size = 0;
        }

        private ulong _freeSpace;
        public ulong FreeSpace
        {
            get { return _freeSpace; }
            set
            {
                if (_freeSpace == value)
                    return;

                _freeSpace = value;

                PropChanged("FreeSpace");
                PropChanged("Description");
            }
        }

        public string Model
        {
            get;
            set;
        }

        public string Name
        {
            get;
            private set;
        }
        
        public ulong Size
        {
            get;
            set;
        }

        public string Volume
        {
            get;
            set;
        }

        public string Description
        {
            get
            {
                var builder = new StringBuilder();
                builder.Append(Name);
                builder.Append(" ");
                builder.Append(Volume);
                builder.Append(" (");
                builder.Append(Model);
                builder.Append(") ");
                builder.Append(" free ");
                builder.Append(FormatBytes(FreeSpace));
                builder.Append(" of ");
                builder.Append(FormatBytes(Size));

                return builder.ToString();
            }
        }

        private string FormatBytes(ulong bytes)
        {
            string format;

            if (bytes < Kb)
            {
                format = String.Format("{0} Bytes", bytes);
            }
            else if (bytes < Mb)
            {
                bytes = bytes / Kb;
                format = String.Format("{0} KB", bytes.ToString("N"));
            }
            else if (bytes < Gb)
            {
                double dree = (double)bytes / Mb;
                format = String.Format("{0} MB", dree.ToString("N1"));
            }
            else
            {
                double gree = (double)bytes / Gb;
                format = String.Format("{0} GB", gree.ToString("N1"));
            }

            return format;
        }
    }
}
